﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
 

namespace MaxLeaf.DAL.Web2SCP
{
   

    public class user_mainDAL : BaseDAL
    {
        private string mstrCompanyFieldNames = "CompanyID,WebUserID,Company_Name,Mobile,eMail,active,mobile_active,ParentID,fax,contact,PostalCode,Address,Register_Time,PassWord";
        private string mstrCompanyFieldNames_Info = "companyID,ProvID,CityID,RegionID,invoicetype,invoicehead,invoiceaddress,invoicephone,invoicebankname,invoicebankacct,Is_Buyer,Is_SeparateAccount";
        private string  mstrUserFieldNames = "";


        public user_mainDAL()
            : base()
        {
            strSCPTableName = "muser_main";
            strWebTableName = "user_main";

            strSCPKeyFieldName = "user_id";
            strWebKeyFieldName = "user_id";

        }

        public override void  Delete (DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestSCPCon, string strKeyValue)
        {
            string strSQL = "";
            strSQL = "UPDATE mCompany SET IS_Delete=1 where webUserID='"+ strKeyValue +"'";
            dbDestSCPCon.ExecuteSql(strSQL); 
          
        }


        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestSCPCon, string strKeyValue)
        {
            string strSQL = "";
            strSQL = "select * from user_main where user_id='" + strKeyValue + "' ";
            DataRow drUser_Main = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drUser_Main == null)
            {
                LogManager.Debug(string.Format("网站不存在用户ID：  " + strKeyValue + ""));
                return 0;
            }
            strSQL = "select * from user_info where user_id='" + strKeyValue + "' ";
            DataRow drUser_info = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drUser_info == null)
            {
                LogManager.Debug(string.Format("网站不存在用户ID：  " + strKeyValue + ""));
                return 0;
            }

            #region 向SCP更新公司信息
            DataRow drNewMain = dbDestSCPCon.GetDataFirstRow("select top 1 * from mCompany where webUserID='" + strKeyValue + "'");
            if (drNewMain == null)
            {
                return Add(dbOrgWebReadCon,   dbOrgWebWriteCon,   dbDestSCPCon,   strKeyValue );
                LogManager.Debug(string.Format("SCP不存在网站用户ID：  " + strKeyValue + ""));
                return 0;
            }
            DataRow drNewInfo = dbDestSCPCon.GetDataFirstRow("select top 1 * from mCompany_info where companyid='" + drNewMain["CompanyID"] + "' ");            
            drNewMain["Company_Name"] = CvtFunc.ToString(drUser_Main["company_name"]);
            drNewMain["Mobile"] = CvtFunc.ToString(drUser_Main["mobile"]);
            drNewMain["eMail"] = CvtFunc.ToString(drUser_Main["eMail"]);
            drNewMain["active"] = CvtFunc.ToString(drUser_Main["active"]);
            drNewMain["mobile_active"] = CvtFunc.ToString(drUser_Main["mobile_active"]);
            drNewMain["PassWord"] = CvtFunc.ToString(drUser_Main["PassWord"]);
            //drNewMain["ParentID"] = dbDestSCPCon.GetValue("select companyID from where webuserid='" + CvtFunc.ToString(drUser_info["parent_id"]) + "'");
            if (CvtFunc.ToInt(drUser_info["parent_id"]) > 0)
            {
                drNewMain["ParentID"] = CvtFunc.ToInt(dbDestSCPCon.GetValue("select companyID from mCompany where webuserid='" + CvtFunc.ToString(drUser_info["parent_id"]) + "'"));
            }
            else
            {
                drNewMain["ParentID"] = 0;
            }

            drNewMain["fax"] = CvtFunc.ToString(drUser_info["fax"]);
            drNewMain["contact"] = CvtFunc.ToString(drUser_info["contact"]);
            drNewMain["PostalCode"] = CvtFunc.ToString(drUser_info["company_zip"]);
            drNewMain["Address"] = CvtFunc.ToString(drUser_info["company_Address"]);
            KeyValuePair<string, IDataParameter[]> kvp = dbDestSCPCon.CreateParamSql(mstrCompanyFieldNames, "mCompany", drNewMain, LogStatus.Update, "RecID");
            dbDestSCPCon.ExecuteSql(kvp.Key, kvp.Value);
            
            drNewInfo["ProvID"] = CvtFunc.ToString(drUser_info["Company_Province"]);
            drNewInfo["CityID"] = CvtFunc.ToString(drUser_info["Company_City"]);
            drNewInfo["RegionID"] = CvtFunc.ToString(drUser_info["Company_Region"]);

            drNewInfo["invoicetype"] = CvtFunc.ToString(drUser_info["tax_type"]);
            drNewInfo["invoicehead"] = CvtFunc.ToString(drUser_info["tax_head"]);
            drNewInfo["invoiceaddress"] = CvtFunc.ToString(drUser_info["tax_company_address"]);
            drNewInfo["invoicephone"] = CvtFunc.ToString(drUser_info["tax_company_tel"]);
            drNewInfo["invoicebankname"] = CvtFunc.ToString(drUser_info["tax_bank_name"]);
            drNewInfo["invoicebankacct"] = CvtFunc.ToString(drUser_info["tax_bank_accounts"]);
            
            drNewInfo["Is_Buyer"] =true;
            drNewInfo["Is_SeparateAccount"] =true;
            
            KeyValuePair<string, IDataParameter[]> kvp_info = dbDestSCPCon.CreateParamSql(mstrCompanyFieldNames_Info, "mCompany_info", drNewInfo, LogStatus.Update, "RecID");
            dbDestSCPCon.ExecuteSql(kvp_info.Key, kvp_info.Value);
            #endregion
            return 0;
        }

        public override int Add(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestSCPCon, string strKeyValue)
        {
            string strSQL = "";
            strSQL = "select * from user_main where user_id='"+ strKeyValue +"' ";
            DataRow drUser_Main = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drUser_Main == null)
            {
                LogManager.Debug(string.Format("网站不存在用户ID：  "+ strKeyValue +""));
                return 0;
            }
            strSQL = "select * from user_info where user_id='" + strKeyValue + "' ";
            DataRow drUser_info = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drUser_info == null)
            {
                LogManager.Debug(string.Format("网站不存在用户ID：  " + strKeyValue + ""));
                return 0;
            }

            
            string strCompanyID = "";
            using (DBAccess dbconID = new DBAccess(dbDestSCPCon.SCPFinterIDDB))
            {
                const int Item = 4;
                strCompanyID = dbconID.GetValue("exec GetICFinteID @IsSave,@FBillType,@BillId output"
                   , new SqlParameter[] {
                        new SqlParameter("@IsSave", "1")
                        ,new SqlParameter("@FBillType",(Item).ToString())
                        ,new SqlParameter("@BillID",SqlDbType.VarChar,50)
                    }
                   , "BillID");
            }

            #region 向SCP添加公司
            DataRow drNewMain = dbDestSCPCon.GetDataTable("select top 0 * from mCompany").NewRow();
            DataRow drNewInfo = dbDestSCPCon.GetDataTable("select top 0 * from mCompany_info").NewRow();
            drNewMain["CompanyID"] = strCompanyID; 
            drNewMain["WebUserID"] = CvtFunc.ToString(drUser_Main["user_id"]);
            drNewMain["Company_Name"] = CvtFunc.ToString(drUser_Main["company_name"]);
            drNewMain["Mobile"] = CvtFunc.ToString(drUser_Main["mobile"]);
            drNewMain["eMail"] = CvtFunc.ToString(drUser_Main["eMail"]);
            drNewMain["active"] = CvtFunc.ToString(drUser_Main["active"]);
            drNewMain["PassWord"] = CvtFunc.ToString(drUser_Main["PassWord"]);
            drNewMain["mobile_active"] = CvtFunc.ToString(drUser_Main["mobile_active"]);
            drNewMain["Register_Time"] = CvtFunc.ToDateTime( CvtFunc.ToLong(drUser_Main["register_time"])); 
            if (CvtFunc.ToInt(drUser_info["parent_id"])>0)
            {
                drNewMain["ParentID"] = CvtFunc.ToInt(dbDestSCPCon.GetValue("select companyID from mCompany where webuserid='" + CvtFunc.ToString(drUser_info["parent_id"]) + "'"));
            }
            else
            {
                drNewMain["ParentID"] = 0;
            }
            drNewMain["fax"] = CvtFunc.ToString(drUser_info["fax"]);
            drNewMain["contact"] = CvtFunc.ToString(drUser_info["contact"]);
            drNewMain["PostalCode"] = CvtFunc.ToString(drUser_info["company_zip"]);
            drNewMain["Address"] = CvtFunc.ToString(drUser_info["company_Address"]);
            KeyValuePair<string, IDataParameter[]> kvp = dbDestSCPCon.CreateParamSql(mstrCompanyFieldNames, "mCompany", drNewMain, LogStatus.Add, "RecID");
            dbDestSCPCon.ExecuteSql(kvp.Key, kvp.Value);       
            
            drNewInfo["CompanyID"] = strCompanyID;
            drNewInfo["ProvID"] = CvtFunc.ToString(drUser_info["Company_Province"]);
            drNewInfo["CityID"] = CvtFunc.ToString(drUser_info["Company_City"]);
            drNewInfo["RegionID"] = CvtFunc.ToString(drUser_info["Company_Region"]);
            drNewInfo["invoicetype"] = CvtFunc.ToString(drUser_info["tax_type"]);
            drNewInfo["invoicehead"] =   CvtFunc.ToString(drUser_info["tax_head"]);
            drNewInfo["invoiceaddress"] =  CvtFunc.ToString(drUser_info["tax_company_address"]);
            drNewInfo["invoicephone"] =  CvtFunc.ToString(drUser_info["tax_company_tel"]);
            drNewInfo["invoicebankname"] =  CvtFunc.ToString(drUser_info["tax_bank_name"]);
            drNewInfo["invoicebankacct"] =  CvtFunc.ToString(drUser_info["tax_bank_accounts"]);

            drNewInfo["Is_Buyer"] = true;
            drNewInfo["Is_SeparateAccount"] = true;

            KeyValuePair<string, IDataParameter[]> kvp_info = dbDestSCPCon.CreateParamSql(mstrCompanyFieldNames_Info, "mCompany_info", drNewInfo, LogStatus.Add, "RecID");
            dbDestSCPCon.ExecuteSql(kvp_info.Key, kvp_info.Value);
            #endregion

            #region 向SCP添加用户
            DataRow drUserMain = dbDestSCPCon.GetDataTable("select top 0 * from mUser_Main").NewRow();
            DataRow drUserInfo = dbDestSCPCon.GetDataTable("select top 0 * from mUser_info").NewRow();
            drUserMain["CompanyID"] = strCompanyID;
            drUserMain["WebUserID"] = drNewMain["WebUserID"];
            string strUserID = "";
            using (DBAccess dbconID = new DBAccess(dbDestSCPCon.SCPFinterIDDB))
            {
                const int Item = 7;
                strUserID = dbconID.GetValue("exec GetICFinteID @IsSave,@FBillType,@BillId output"
                   , new SqlParameter[] {
                        new SqlParameter("@IsSave", "1")
                        ,new SqlParameter("@FBillType",(Item).ToString())
                        ,new SqlParameter("@BillID",SqlDbType.VarChar,50)
                    }
                   , "BillID");
            }
            drUserMain["UserID"] = strUserID ;
            drUserMain["UserName"] = drNewMain["Company_Name"];
            drUserMain["PassWord"] = drUser_Main["PassWord"];
            drUserMain["Is_Admin"] =true;
            drUserMain["PermitGroup"] = "1";
            string strUser_Main = "CompanyID,WebUserID,UserID,UserName,PassWord,Is_Admin,PermitGroup";
            KeyValuePair<string, IDataParameter[]> kvpUser_Main = dbDestSCPCon.CreateParamSql(strUser_Main, "muser_main", drUserMain, LogStatus.Add, "RecID");
            dbDestSCPCon.ExecuteSql(kvpUser_Main.Key, kvpUser_Main.Value);

            drUserInfo["CompanyID"] = strCompanyID;
            drUserInfo["UserID"] = strUserID;
            drUserInfo["phone"] = CvtFunc.ToString(drUser_Main["mobile"]);
            drUserInfo["fax"] = CvtFunc.ToString(drUser_info["fax"]);
            drUserInfo["eMail"] = CvtFunc.ToString(drUser_Main["eMail"]);
            string strUser_info = "CompanyID,UserID,phone,fax,eMail";
            KeyValuePair<string, IDataParameter[]> kvpUser_info = dbDestSCPCon.CreateParamSql(strUser_info, "muser_info", drUserInfo, LogStatus.Add, "RecID");
            dbDestSCPCon.ExecuteSql(kvpUser_info.Key, kvpUser_info.Value);
            #endregion

            
            strUserID = dbDestSCPCon.GetValue("exec P_InitCompanyData @FromCompanyId,@ToCompanyId"
                   , new SqlParameter[] {    new SqlParameter("@FromCompanyId", "-1")
                                            ,new SqlParameter("@ToCompanyId",strCompanyID)  });

            
            return 1;
        }

              
    }
}